package com.dq.rbac.commons.security;

import cn.hutool.json.JSONUtil;
import com.dq.rbac.vo.R;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @project: rbac
 * @ClassName: 自定义登出功能
 * @author: dq
 * @creat: 2022/12/27 12:56
 */
@Component
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        //jwt Error错误返回
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        ServletOutputStream outputStream = httpServletResponse.getOutputStream();
        byte[] result = JSONUtil.toJsonStr(R.ok().message("退出成功")).getBytes(StandardCharsets.UTF_8);
        outputStream.write(result);
        outputStream.flush();
        outputStream.close();
    }
}
